package ar.com.solidussource.howmuchmobile.services;

import ar.com.solidussource.howmuchmobile.common.entities.Productcategory;
import ar.com.solidussource.howmuchmobile.common.entities.Productsubcategory;
import ar.com.solidussource.howmuchmobile.common.exception.ServiceException;
import ar.com.solidussource.howmuchmobile.services.helper.RestServiceHelper;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 07/11/12
 * Time: 15:10
 * To change this template use File | Settings | File Templates.
 */
public class ProductsubcategoryRestService implements RestService {
    private static String SERVICE_ENDPOINT = SERVER_ENDPOINT+"/productsubcategory";
    private static String OP_LIST_ENDPOINT = SERVICE_ENDPOINT+"/list.json";

    public List<Productsubcategory> getProductsubcategories(Integer productcategoryId, Integer pageNumber, Integer size) throws ServiceException {
        List<Productsubcategory> productsubcategories = new ArrayList<Productsubcategory>();

        HttpClient httpClient = new DefaultHttpClient();

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("productcategoryId", productcategoryId);
        params.put("pageNumber", pageNumber);
        params.put("size", size);

        String url = RestServiceHelper.addParamToUrl(OP_LIST_ENDPOINT, params);

        HttpGet get = new HttpGet(url);

        try{
            HttpResponse resp = httpClient.execute(get);
            String respStr = EntityUtils.toString(resp.getEntity());

            JSONArray respJSON = new JSONArray(respStr);

            for(int i=0; i<respJSON.length(); i++) {
                JSONObject productsubcategoryJSON = respJSON.getJSONObject(i);

                Productsubcategory productsubcategory = new Productsubcategory();
                productsubcategory.setId(Integer.valueOf(productsubcategoryJSON.getInt("id")));
                productsubcategory.setName(productsubcategoryJSON.getString("name"));
                productsubcategory.setDescription(productsubcategoryJSON.getString("description"));

                JSONObject productcategoryJSON = productsubcategoryJSON.getJSONObject("productcategory");

                Productcategory productcategory = new Productcategory();
                productcategory.setId(Integer.valueOf(productcategoryJSON.getInt("id")));
                productcategory.setName(productcategoryJSON.getString("name"));
                productcategory.setDescription(productcategoryJSON.getString("description"));

                productsubcategory.setProductcategory(productcategory);

                productsubcategories.add(productsubcategory);
            }

        } catch (Exception e) {
            throw new ServiceException(e);
        }

        return productsubcategories;
    }

    public Productsubcategory[] getProductcategoriesAsArray(Integer productcategoryId, Integer pageNumber, Integer size) throws ServiceException {
        List<Productsubcategory> productsubcategories = getProductsubcategories(productcategoryId, pageNumber, size);

        Productsubcategory[] productsubcategoriesAsArray = new Productsubcategory[productsubcategories.size()];

        int i = 0;

        Iterator<Productsubcategory> ite = productsubcategories.iterator();

        while (ite.hasNext()) {
            Productsubcategory productsubcategory = ite.next();
            productsubcategoriesAsArray[i] = productsubcategory;
            i++;
        }

        return productsubcategoriesAsArray;
    }
}
